package com.fhj.movie.baidumap;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

public class LocationServices extends Service {

	private final IBinder mBinder =new LocationBinder();
	
	private  LocationClient mLocationClient = null;//初始化LocationClient类
	private  BDLocationListener mylisListener = new MyLocationListener();
	
	private ServiceCallBack callBack = null;
	
	public void setServiceCallBack(ServiceCallBack callBack){
		this.callBack = callBack;
	}

	
	@Override
	public void onCreate() {
		// TODO 自动生成的方法存根
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO 自动生成的方法存根
		Log.d("onStartCommand", "onStartCommand :"+startId+","+flags);
		getLocation();
		return super.onStartCommand(intent, flags, startId);
	}
	
	public class LocationBinder extends Binder{
		
		 public LocationServices getServices(){
			return LocationServices.this;
		}
		
	}
	
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("服务已经被绑定");
		return mBinder;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		// TODO 自动生成的方法存根
		System.out.println("服务已经取消绑定");
		return true;
	}
	
	
	/**
	 * 客户端要调用的方法
	 */
	public  void getLocation(){
		
		mLocationClient = new LocationClient(getApplicationContext());
		
		LocationClientOption option = new LocationClientOption();
		
		option.setLocationMode(LocationMode.Hight_Accuracy);
		option.setCoorType("bd09ll");
//		option.setScanSpan(5000);
		option.setIsNeedAddress(true);
		mLocationClient.setLocOption(option);
	
		mLocationClient.registerLocationListener(mylisListener);
		mLocationClient.start();
		
		Toast.makeText(getApplicationContext(), "hehe", Toast.LENGTH_SHORT).show();
		
		Log.d("", "======getLocation()");
		
	}
	
	
	private class MyLocationListener implements BDLocationListener{

		
		
		@Override
		public void onReceiveLocation(BDLocation location) {
			// TODO Auto-generated method stub
			if (location==null) {
				
				Toast.makeText(getApplicationContext(), "网络定位失败", Toast.LENGTH_SHORT).show();
			}else {
//				Toast.makeText(getApplicationContext(), "Services"+location.getLocType()+","+location.getLatitude()+","+location.getLongitude()+","+location.getAddrStr(), Toast.LENGTH_SHORT).show();
				callBack.getLocationInfo(location.getLatitude(), location.getLongitude(), location.getAddrStr());
			}
		}
		
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mLocationClient.stop();
		mLocationClient.unRegisterLocationListener(mylisListener);
	}

	public interface ServiceCallBack{
		public void getLocationInfo(double latitude,double longitude,String address);
	}
	
}
